<?php

/**
 * Description of router
 *
 * @author user
 */
class Router {

    private $_segments = array();
    private $_controller;
    private $_method;
    private $_vars = array();

    /**
     * Constructor
     */
    public function __construct() {
        $this->_set_uri();
        $this->_set_controller();
        $this->_set_method();
        $this->_set_vars();
    }

    /**
     * Get URI Request
     */
    private function _set_uri() {
        $uri_string = str_replace($_SERVER["REQUEST_URI"], "", $_SERVER["SCRIPT_NAME"]);

        if ($uri_string == 'index.php') { // index.php is writen
            $uri_string = '';
        } else {
            $uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']);

            // Delete slash at begin and end
            $uri_string = preg_replace("|/*(.+?)/*$|", "\\1", str_replace("\\", "/", $uri_string));
            $uri_string = trim($uri_string, '/');
        }

        // Split uri to segments
        $this->_segments = preg_split("[\\/]", $uri_string, 0, PREG_SPLIT_NO_EMPTY);
    }

    /**
     * Search controller class
     */
    private function _set_controller() {
        
        if (!isset($this->_segments[0])) {  // Controller is not set
            require BASEPATH . 'config/config.php';

            // Get Default Controller
            $this->_segments[0] = $config['default_controller'];
        }

        $controller_path = BASEPATH . 'controllers/' . $this->_segments[0] . '.php';

        if (file_exists($controller_path)) {    // Controller file is exist
            require_once BASEPATH . 'core/controller.php';
            require_once $controller_path;
            $class = ucfirst($this->_segments[0] . 'Controller');

            if (!class_exists($class)) {    // Controller class is exist
                show_error("Controller $class could not be found.");
            }

            $this->_controller = new $class();
        } else {
            show_error("File $controller_path could not be found.");
        }
    }

    /**
     * Set Method
     */
    private function _set_method() {
        // If method is not defined, call index
        if (!isset($this->_segments[1]))
            $this->_segments[1] = 'index';

        if (method_exists($this->_controller, $this->_segments[1])) {   // Method is exist
            $this->_method = $this->_segments[1];

            if (substr($this->_method, 0, 1) == '_') {  // Method is private
                show_error("Method $this->_method could not be found.");
            }
        } else {
            show_error();
        }
    }

    /**
     * Set method's parameter
     */
    private function _set_vars() {
        if (!isset($this->_segments[2])) {
            $this->_vars = array_slice($this->_segments, 2);
        }
    }

    /**
     * Do request
     */
    public function do_request() {
        call_user_func_array(array(&$this->_controller, $this->_method), $this->_vars);
        $this->_controller->render_view();
    }

    /**
     * Get Segment
     * @return ArrayOfString
     */
    public function get_segment() {
        return $this->_segments;
    }
}

?>
